# -*- coding: cp936 -*-
import socket
import os

BUFFERSIZE = 1024

host = socket.gethostbyname(socket.gethostname())
print(host)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, 6888))
server.listen(1)
print("waiting for connect......")
TcpCliSock, addr = server.accept()
while 1:
    data = TcpCliSock.recv(BUFFERSIZE)
    if data:
        print(data)
        if 'MOVE' in data:
            print("receive command: MOVE")
            TcpCliSock.send('PRMOVE:1\n'.encode("utf-8"))

        elif data.startswith('PRLOAD'):
            print("receive command: PRLOAD")
            TcpCliSock.send('PRLOAD:1\n'.encode("utf-8"))

        elif data.startswith('PRUNLOAD'):
            print("receive command: PRUNLOAD")
            TcpCliSock.send('PRUNLOAD:1\n'.encode("utf-8"))

        elif data.startswith('PRCHUCK'):
            print("receive command: PRCHUCK")
            TcpCliSock.send('PRCHUCK:1\n'.encode("utf-8"))

        elif data.startswith('PRUNCHUCK'):
            print("receive command: PRUNCHUCK")
            TcpCliSock.send('PRUNCHUCK:1\n'.encode("utf-8"))

        elif 'next' in data:
            print('receive command: next')
            TcpCliSock.send('next cmd completed\n'.encode("utf-8"))
        elif 'end' in data:
            print('receive command: end')
            #            str = "get"+data
            TcpCliSock.send('end cmd completed\n'.encode("utf-8"))
        elif "EXIT" in data or "exit" in data:
            TcpCliSock.send('exit\n'.encode("utf-8"))
            print('software exit now!')
            break
        else:
            str1 = 'unknown command:' + data
            print(str1)
            TcpCliSock.send(('%s\n' % str1).encode("utf-8"))

TcpCliSock.close()
server.close()
